<?php



// diy套装列表
function diylist($json , $session)
{
	$offset = 0 ;	$limit = 10 ;
	util_get_PAGINATION($json , $offset , $limit) ;
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	$room 	= get_jsonValue($filter , "room") ;			$addwhere .= _filter_room_set(intval($room)) ;		// 空间
	$style	= intval(get_jsonValue($filter , "style")) ;
	$color 	= intval(get_jsonValue($filter , "color")) ;
	
	$type	= intval(get_jsonValue($json,"type")) ;	//** 0 所有模板 ** ** 1 我的列表 **
	if ($type == 1){
		$uid = util_set_GETUID_for_SESSION($session) ;
		if ($uid <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
			return $ret ;
		}
	}
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	// 组织页面数据
	$start = ($offset - 1) * $limit ;	
	$ret['data']['pagination'] = array() ;
	
	if ($type == 0){
		if ($style > 0)	$addwhere .= " and `id` in (select `diy_id` from diy_style where `style_id` = '$style')" ; 		// 风格
		if ($color > 0)	$addwhere .= " and `id` in (select `diy_id` from diy_color where `color_id` = '$color')" ;		// 色系
		
		$allcount = sql_fetch_one_cell("select count(`id`) from `diy` where `app_return` = 1 $addwhere") ;	
		util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
		
		$bitem = sql_fetch_rows("select * from `diy` where `app_return` = 1 $addwhere order by `top_time` desc , `id` desc limit $start , $limit") ;	
		foreach ($bitem as $b){
			$rn = array() ;
			util_set_SET_SIMPLE_diy($rn , $b) ;
			$ret['data']['set'][] = $rn ;
		}
	}else if ($type == 1){
		if ($style > 0)	$addwhere .= " and `user_diy`.`diy_id` in (select `diy_id` from diy_style where `style_id` = '$style')" ; 		// 风格
		if ($color > 0)	$addwhere .= " and `user_diy`.`diy_id` in (select `diy_id` from diy_color where `color_id` = '$color')" ;		// 色系
		
		$allcount = sql_fetch_one_cell("select count(`user_diy`.`id`) from `user_diy` left join `diy` on `user_diy`.`diy_id` = `diy`.`id` where `user_id` = '$uid' $addwhere") ;
		util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
		
		$bitem = sql_fetch_rows("select user_diy.*,diy.space_id from `user_diy` left join `diy` on `user_diy`.`diy_id` = `diy`.`id` where `user_id` = '$uid' $addwhere order by `create_time` desc , `id` desc limit $start , $limit") ;	
		foreach ($bitem as $b){
			$rn = array() ;
			util_set_SET_SIMPLE_diy($rn , $b) ;
			$ret['data']['set'][] = $rn ;
		}
	}
		
	return $ret ;
}
// diy套装 
function diy($json, $session)
{
	$id = intval(get_jsonValue($json,"id")) ;		// 编号
	$type = intval(get_jsonValue($json,"type")) ;	// 请求类型 ** 0 模板 ** ** 1 我的列表 **
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	
	if ($type == 0){
		$item = sql_fetch_one("select * from `diy` where `id` = '$id'") ;
	}elseif ($type == 1){
		$uid = util_set_GETUID_for_SESSION($session) ;
		if ($uid <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
			return $ret ;
		}
		$item = sql_fetch_one("select * from `user_diy` where `id` = '$id'") ;
	}
	if (is_null($item) || array_key_exists('id' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}
	
	if ($type == 0){
		util_set_SET_diy($ret['data']['set'] , $item) ;
	}elseif ($type == 1){
		util_set_SET_my_diy($ret['data']['set'] , $item) ;
	}
	return $ret ;
}

// diy 单品列表
function diy_itemlist($json , $session , $new_v2 = false)
{
	$type	= intval(get_jsonValue($json,"type")) ;	//** 0 默认模板+收藏夹 **  ** 1 所有列表 **
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($type == 0){
		if ($uid <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
			return $ret ;
		}
	}
	$id = intval(get_jsonValue($json,"id")) ;			// pos表的编号
	$id_type = intval(get_jsonValue($json,"id_type")) ;	// 请求类型 ** 0 模板 ** ** 1 我的列表 **
	if ($id_type == 1){
		if ($uid <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
			return $ret ;
		}
	}	

	$is_my_diy = 1 ;
	// 获取对应的空间和列表
	if (intval($id_type) == 0){
		$ones = sql_fetch_one("select d.`space_id` , s.`class_id` , p.* from diy_pos p inner join diy d on p.diy_id = d.id inner join sys_item s on p.`item_id` = s.`id` where p.id = '$id'") ;
		if (empty($ones) || array_key_exists("space_id", $ones) == false){
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
			return $ret ;
		}
	}else{
		$val = sql_fetch_one("select u.`diy_id` , p.* from `user_diy` u inner join `user_diy_pos` p on u.`id` = p.`user_diy_id` where u.`user_id` = '$uid' and p.`id` = '$id'") ;
		
		$ones = sql_fetch_one("select d.`space_id` , s.`class_id` , p.* from diy_pos p inner join diy d on p.diy_id = d.id inner join sys_item s on p.`item_id` = s.`id` where p.id = '{$val['pos_id']}'") ;
		if (empty($ones) || array_key_exists("space_id", $ones) == false){
			$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
			return $ret ;
		}
		$ones['item_id'] 	= $val['item_id'] ;
		$ones['pos_id'] 	= $ones['id'] ;
		$ones['id']			= $val['id'] ;		
		$ones['img_id']		= $val['img_id'] ;

		$is_my_diy = 2 ;
	}
	$classid = $ones['class_id'] ;
	$spaceid = $ones['space_id'] ;

	$cla_list = trim(sql_fetch_one_cell("select grp_list from diy_class_grp where class_id = '$classid'")) ;
	$class_str_list = "$classid" ;
	if (strlen($cla_list) > 0)	$class_str_list .= " , $cla_list" ;
	
	temp_check_and_createtable__mem_diy_tz_item() ;	
	
	$addwhere = "" ;
	$filter = get_jsonValue($json,"filter") ;
	// 品牌过滤
	$brand 	= get_jsonValue($filter , "brand") ;

	//20181026 新增品牌的单品在配置的城市显示 -start
	$city_filter = trim(get_jsonValue($json,"cityname")) ;
	if (strlen($city_filter) >= 2){
		$brand_lists = array() ;
		$brand_lists = sql_fetch_rows("select b.* from grp_brand_city b inner join grp_city c on b.city_id = c.id inner join grp_brand d on b.brand_id = d.id and d.`useing` = 1 where c.`name` = '$city_filter'") ;
		$brands = "" ; $dh = "" ;
		foreach($brand_lists as $b){
			$brands .= $dh.$b['brand_id'] ; $dh = "," ;
		}
		//if ($brands != "")
		{
			$other_list = sql_fetch_rows("SELECT * from grp_brand a where  a.`useing` = 1 and  a.id not in (select e.brand_id from grp_brand_city e )") ;
			foreach($other_list as $b){
				$brands .= $dh.$b['id'] ; $dh = "," ;
			}

			$addwhere .= " and i.`brand_id` in ($brands) " ;	
		}
	}		
	//-end
	
	if ($brand > 0){
		if ($brand < 10000){
			$addwhere .= " and i.`brand_id` = '$brand' " ;
		}else{
			if ($brand % 10000 == 0){
				$brand = $brand / 10000 ;
				$addwhere .= " and i.`brand_id` in (select `id` from grp_brand where head_grp = '$brand' and `useing` = 1) " ;
			}else{
				// 某个品牌总的某个系列
				$seryid = intval($brand / 10000) ;	// 系列id
				$brand = intval($brand % 10000) ;		// 品牌id
				
				$addwhere = " and i.`brand_id` = '$brand' and i.`id` in (select `item_id` from sys_item_sery where `sery_id` = '$seryid') " ;
			}
		}
	}else{
		$addwhere .= " and i.`brand_id` in (select `id` from grp_brand where `useing` = 1) " ;
	}
	// 风格过滤
	$style	= get_jsonValue($filter , "style") ;
	if ($style > 0){
		$addwhere .= " and i.`id` in (select `item_id` from sys_item_style where `style_id` in (SELECT id FROM `grp_style` where id ='$style'  or father='$style') group by `item_id`)" ;
	}
	// 色系过滤
	$color 	= get_jsonValue($filter , "color") ;
	if ($color > 0){
		$addwhere .= " and i.`id` in (select `item_id` from sys_item_color where `color_id` = '$color' group by `item_id`)" ;
	}
	// 价格过滤
	$price	= get_jsonValue($filter , "price") ;		
	$price_min = get_jsonValue($price , "min") ;
	$price_max = get_jsonValue($price , "max") ;
	if ($price_min != 0 || $price_max != 0){
		// 这里只判断基础折扣价.
		$addwhere .= " and i.`price_base` * i.`price_discount` >= $price_min and i.`price_base` * i.`price_discount` <= $price_max " ;
	}
	if ($type == 0){
		// 默认模板+收藏夹中的可换单品.
		$addwhere .= " and i.`id` in (select `type_id` from user_favorite where uid = '$uid' and `type` = 1) or (i.`id` = '{$ones['item_id']}')" ;
	}

	
	// 获取请求的分页数据
	$offset = 0 ;	$limit = 10 ;
	util_get_PAGINATION($json , $offset , $limit) ;
	
	
	$ret = array() ;
	$ret['data'] = array() ;
	$ret['data']['list'] = array() ;
	
	if ($offset < 1)	$offset = 1 ;
	// 组织页面数据
	$start = ($offset - 1) * $limit ;
	
	// 是否只返回某个状态的单品过滤	
	$state_tree = 0 ;
	$state_where = " i.`state_tree` = '$state_tree' and " ;

	$allcount = sql_fetch_one_cell("select count(i.`id`) from `sys_item` i inner join mem_diy_tz_item m on $state_where i.`id` = m.`item_id` inner join `sys_item_space` s on i.`id` = s.`item_id` and s.`space_id` = '$spaceid' and i.`app_return` <= 1 where i.`class_id` in ($class_str_list) $addwhere order by i.`top_time` desc , i.`update_at` desc") ;
	$ret['data']['pagination'] = array() ;
	util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select i.* from `sys_item` i inner join mem_diy_tz_item m on $state_where i.`id` = m.`item_id` inner join `sys_item_space` s on i.`id` = s.`item_id` and s.`space_id` = '$spaceid' and i.`app_return` <= 1 where i.`class_id` in ($class_str_list) $addwhere order by i.`top_time` desc , i.`update_at` desc , i.`id` desc limit $start , $limit") ;
	foreach ($bitem as $b){
		$rn = array() ;
		
		if ($new_v2 == true){
			util_set_SET_POS_ITEM_diy($rn , $ones , $b , $uid , $is_my_diy , true) ;
		}else{
			util_set_SET_POS_ITEM_diy($rn , $ones , $b , $uid , $is_my_diy) ;
		}
		$rn['id'] = $id ;	
		$ret['data']['list'][] = $rn ;
	}
	$ret['data']['id'] = $id ;
	return $ret ;
}

// diy 单品列表
function diy_itemlist_v2($json , $session)
{
	return diy_itemlist($json , $session , true) ;
}

//  删除DIY
function mydiy_del($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id 		=	intval(get_jsonValue($json,"id")) ;	
	if (sql_check("select `id` from `user_diy` where `id` = '$id' and user_id = '$uid'")){
		sql_query("delete from `user_diy` where `id` = '$id'") ;
		sql_query("delete from `user_diy_pos` where `user_diy_id` = '$id'") ;
	}else{
		$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
		return $ret ;
	}
	
	if (sql_check("select `id` from `user_diy` where `id` = '$id' and user_id = '$uid'")){
		$ret['status'] = $GLOBALS["ERR"]["ERR_DATABASE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_DATABASE"] ;
		return $ret ;
	}
	
	$ret['data'] = array() ;
	return $ret ;
}

// 保存DIY
function mydiy_save($json , $session)
{
	$uid = util_set_GETUID_for_SESSION($session) ;
	$ret = array() ;
	if ($uid <= 0){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$id 		=	intval(get_jsonValue($json,"id")) ;		// 编号 , 为空或0,表示新的diy保存,>0表示mydiy的修改
	$name		= 	trim(get_jsonValue($json,"name")) ;	 	// 名字 , 为空,表示用模板中的名字
	$desc 		=	trim(get_jsonValue($json,"desc")) ;	 	// 描述 , 为空,表示用模板中的描述
	$diy_id 	= 	intval(get_jsonValue($json,"diy_id")) ;	// diy 模板编号
	$diy_pos 	=	get_jsonValue($json,"diy_pos") ; 		// diy 位置信息

	if ($id > 0){
		$diy_id = sql_fetch_one_cell("select diy_id from user_diy where id = '$id'") ;
	}

	$defdiy	 = sql_fetch_one("select * from diy where `id` = '$diy_id'") ;
	if (is_null($defdiy) == true || is_array($defdiy) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
		return $ret ;
	}
	if (strlen($name) == 0)		$name = $defdiy['name'] ;
	if (strlen($desc) == 0)		$desc = $defdiy['tips'] ;
	
	$is_new_id = false ;
	$mydiyone = sql_fetch_one("select * from user_diy where `id` = '$id' and `user_id` = '$uid'") ; 
	if (is_null($mydiyone) == true || is_array($mydiyone) == false){
		// 不存在,创建一个.
		$id = sql_insert("insert into user_diy(`user_id` , `diy_id` , `name` , `tips` , `img_small_id` , `img_thumb_id` , `img_url_id` , `create_time`) values ('$uid' , '$diy_id' , '$name' , '$desc' , 0 , 0 , 0 , unix_timestamp())") ;
		if ($id <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_DATABASE"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_DATABASE"] ;
			return $ret ;
		}
		$is_new_id = true ;
		// 先插入默认模板.
		sql_query("insert into user_diy_pos (`user_diy_id` , `pos_id`  , `img_id` , `item_id`) select '$id' , `id` , `img_id` , `item_id` from `diy_pos` where `diy_id` = '$diy_id'") ;
	}else{
		sql_query("update user_diy set `name` = '$name' , `tips` = '$desc' where `id` = '$id' and `user_id` = '$uid'") ;
	}

	// 更新模板位置表中的数据
	foreach ($diy_pos as $p){
		$v = util_diy_DIY_POS_from_json($p) ;
		$set = "" ; $dh = "" ;
		if (intval($v[1]) > 0){ $set .= " $dh `img_id` = '{$v[1]}'" ;	$dh = "," ; }
		if (intval($v[2]) > 0){ $set .= " $dh `item_id` = '{$v[2]}'" ;	$dh = "," ; }
		if ($set != ""){
			if ($is_new_id == true){
				sql_query("update user_diy_pos set $set where `user_diy_id` = '$id' and `pos_id` = '{$v[0]}'") ;
			}else{
				sql_query("update user_diy_pos set $set where `id` =  '{$v[0]}'") ;
			}
		}
	}
	
	try{
		$img = new set_image() ;
		$img->createAndSave_MyDiy($id) ;
	}catch(Exception $e){}
	
	$ret['data'] = array() ;
	$ret['data']['set'] = array() ;
	$item = sql_fetch_one("select * from `user_diy` where `id` = '$id'") ;
	if (is_null($item) || array_key_exists('id' , $item) == false){
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NO_DATA"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NO_DATA"] ;
		return $ret ;
	}	
	util_set_SET_my_diy($ret['data']['set'] , $item) ;
	return $ret ;
}

// 把fromuid的user_diy表中id=$id的记录分享给用户touid
function diy_copy_to_other($json)
{
	$fromuid = intval(get_jsonValue($json,"fromuid")) ;
	$touid  = intval(get_jsonValue($json,"touid")) ;
	$id = intval(get_jsonValue($json,"id")) ;
	
	$ret = array() ;
	$item = sql_fetch_one("select * from `user_diy` where `id` = '$id' and `user_id` = '$fromuid'") ;
	if (is_null($item) || array_key_exists('id' , $item) == false){
		$ret['status'] = -1 ;
		$ret['message'] = "来源不存在" ;
		return $ret ;
	}	
	$itemto = sql_fetch_one("select * from `user_diy` where `from_id` = '$id' and `user_id` = '$touid'") ;
	if (array_key_exists('id' , $itemto) == true){
		$ret['status'] = -2 ;
		$ret['message'] = "已经分享给此用户了." ;
		return $ret ;
	}
	$diy_id = $item['diy_id'] ;
	$toid = sql_insert("insert into user_diy" .
			"(`user_id` , `diy_id` , `name` , `tips` , " .
			"`img_small_id` , `img_thumb_id` , `img_url_id` , `" .
			"create_time` , `from_uid` , `from_id`) " .
			"values " .
			"('$touid' , '{$item['diy_id']}' , '{$item['name']}' , '{$item['tips']}' , " .
			"{$item['img_small_id']} , {$item['img_thumb_id']} , {$item['img_url_id']} , " .
			"{$item['create_time']} , '$fromuid' , '$id')") ;
	if (intval($toid) <= 0){
		$ret['status'] = -3 ;
		$ret['message'] = "分享失败." ;
		return $ret ;
	}
	
	sql_query("insert into `user_diy_pos` (`user_diy_id` , `pos_id` , `img_id` , `item_id`) select $toid , `pos_id` , `img_id` , `item_id` from `user_diy_pos` where `user_diy_id` = $id ") ;
	
	$ret['message'] = "成功" ;
	return $ret ;
}

// diy销售清单
function diy_item_list($json , $session)
{
	$ret = array() ;
	$diy_type	= intval(get_jsonValue($json,"diy_type")) ;	//** 请求类型 ** 0 模板 ** ** 1 我的列表 **
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($diy_type == 1){
		if ($uid <= 0){
			$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
			return $ret ;
		}
	}
	$ret['data'] = array() ;
	$ret['data']['item_list'] = array() ;
	$idlist = get_jsonValue($json,"id_list") ;
	foreach($idlist as $id){
		$pos_id = get_jsonValue($id ,'pos_id') ;
		$item_id = get_jsonValue($id ,'item_id') ;
		$is_my_diy = 1 ;
		
		$ones = null ;
		// 获取对应的空间和列表
		if (intval($diy_type) == 0){
			$ones = sql_fetch_one("select * from diy_pos where id = '$pos_id'") ;
			if (empty($ones) || array_key_exists("id", $ones) == false){
				$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
				$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
				return $ret ;
			}
			$ones['item_id'] 	= $item_id ;
		}else{
			$val = sql_fetch_one("select u.`diy_id` , p.* from `user_diy` u inner join `user_diy_pos` p on u.`id` = p.`user_diy_id` where u.`user_id` = '$uid' and p.`id` = '$pos_id'") ;
			if (empty($val) || array_key_exists("diy_id", $val) == false){
				$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
				$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
				return $ret ;
			}
			$ones = sql_fetch_one("select * from diy_pos where id = '{$val['pos_id']}'") ;
			if (empty($ones) || array_key_exists("id", $ones) == false){
				$ret['status'] = $GLOBALS["ERR"]["ERR_NOFIND_VALER"] ;
				$ret['message'] = $GLOBALS["MES"]["ERR_NOFIND_VALER"] ;
				return $ret ;
			}
			$ones['item_id'] 	= $item_id ;
			$ones['pos_id'] 	= $ones['id'] ;
			$ones['id']			= $val['id'] ;		
			$ones['img_id']	= $val['img_id'] ;
	
			$is_my_diy = 2 ;
		}
		
		$rn = array() ;
		$bitem = sql_fetch_one("select * from `sys_item` where id = $item_id") ;		
		util_set_SET_POS_ITEM_diy($rn , $ones , $bitem , $uid , $is_my_diy , true) ;			
		$ret['data']['item_list'][] = $rn ;
	}
	return $ret ;
}

?>